function [] = register_exp(ExpID,adata_dir,dvh)

ud=get(dvh.hf,'UserData');
rdata_dir=ud.rdata_dir;
tmp_data_dir=ud.tmp_data_dir;
template_frames = 1:20;

[adata_file]=find_adata_file(ExpID,adata_dir);

try
    registermode = evalin('base','registermode');
catch
    registermode = 1;
end

if ~isempty(adata_file)
    disp('Adata file already exists. Aborting.')
    return
end

ExpInfo = read_info_from_ExpLog(ExpID);

try
    nbr_piezo_layers=readini([rdata_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ...
        ExpInfo.fnames{1}(1:end-3) 'ini'],'piezo.nbrlayers');
catch
    nbr_piezo_layers=1;
    disp('ATTENTION - could not read nbr of piezo layers from ini file')
end

try
    fpga_frames_zstep=readini([rdata_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ...
        ExpInfo.fnames{1}(1:end-3) 'ini'],'FPGA.framesperzstep');
    if fpga_frames_zstep > 0
        nbr_piezo_layers=1;  % more likely setting was forgotten to be set to 1
    end
    fpga_frames_layer=readini([rdata_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ...
        ExpInfo.fnames{1}(1:end-3) 'ini'],'FPGA.framesperlayer');
catch
    disp('ATTENTION - could not read z step details from ini file')
    fpga_frames_zstep=0;
    fpga_frames_layer=1;
end

% load the 2P data
% pre-allocate space for data
for knd=1:length(ExpInfo.fnames)
    finfo=dir([tmp_data_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ExpInfo.fnames{knd}]);
    fi=fopen([tmp_data_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ExpInfo.fnames{knd}],'r');
    x_res=fread(fi,1,'int16=>double');
    y_res=fread(fi,1,'int16=>double');
    nbr_frames(knd)=round(finfo.bytes/x_res/y_res/2);
    nbr_frames(knd)=nbr_frames(knd)-rem(nbr_frames(knd),nbr_piezo_layers);
    fclose(fi);
end
number_of_frames_total=sum(nbr_frames);
if nbr_piezo_layers>1 && registermode == 1
    data=cell(1,nbr_piezo_layers);
    for knd=1:nbr_piezo_layers
        data{knd}=zeros(x_res,y_res,round(number_of_frames_total/4),'int16');
    end
else
    data=zeros(x_res,y_res,number_of_frames_total,'int16');
end

% load the data
for ind=1:length(ExpInfo.fnames)
    if exist([tmp_data_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ExpInfo.fnames{ind}],'file');
        disp(['loading file ' ExpInfo.fnames{ind} ' from local tmpdata directory']);
        curr_load_path=[tmp_data_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ExpInfo.fnames{ind}];
    else
        disp(['loading file ' ExpInfo.fnames{ind} ' from the network RawData directory']);
        curr_load_path=[rdata_dir ExpInfo.userID '\' ExpInfo.mouse_id '\' ExpInfo.fnames{ind}];
    end
    
    fi=fopen(curr_load_path,'r');
    x_res=fread(fi,1,'int16=>double');
    y_res=fread(fi,1,'int16=>double');
    
    if nbr_piezo_layers>1 && registermode == 1
        for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
            data{rem(knd-1,nbr_piezo_layers)+1}(:,:,floor((knd-1)/nbr_piezo_layers)+1)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
        end
    else
        for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
            data(:,:,knd)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
        end
    end
    fclose(fi);
end

if fpga_frames_zstep>0
    disp('Now registering Z stack');
    mean_data=register_multilayer(data,fpga_frames_layer);
    mean_data_fname=[adata_dir userID '\' mouse_id '\mean_data-' ExpInfo.fnames{1}(1:end-10) '.mat'];
    if ~isdir([adata_dir userID '\' mouse_id])
        mkdir([adata_dir userID], mouse_id);
    end
    save(mean_data_fname,'mean_data');
    return;
end



if nbr_piezo_layers<2
    disp(['Now registering the data on frames ' num2str(template_frames(1)) ':' num2str(template_frames(end))]);
    [dx,dy]=register_frames(data,mean(data(:,:,template_frames),3));
elseif nbr_piezo_layers > 1 && registermode == 1
    dx={};
    dy={};
    for ynd=1:nbr_piezo_layers
        disp(['Now registering the data on frames ' num2str(template_frames(1)) ':' num2str(template_frames(end)) ' ' 'z-plane:' ' ' num2str(ynd)]);
        [dx_tmp,dy_tmp]=register_frames(data{ynd},mean(data{ynd}(:,:,template_frames),3));
        dx{ynd}=dx_tmp;
        dy{ynd}=dy_tmp;
    end
elseif nbr_piezo_layers > 1 && registermode == 2
    dx={};
    dy={};
    disp(['Now registering the data (layer avg) on frames ' num2str(template_frames(1)) ':' num2str(template_frames(end))]);
    [dx_tmp,dy_tmp]=register_frames(avgStack(data,nbr_piezo_layers),mean(data(:,:,template_frames),3));
    for ynd=1:nbr_piezo_layers
        dx{ynd}=dx_tmp;
        dy{ynd}=dy_tmp;
    end
end

disp(['Now registering data on dx dy values and correcting line shift']);   
if ~isa(dx,'cell')
    data=shift_data(data,dx,dy);
    data=correct_line_shift(data,mean(data,3));
    act_map=calc_act_map(data);
    template=mean(data,3);
elseif registermode == 2
    act_map = {};
    template = {};
    data = shift_data(data,expmat(dx_tmp,nbr_piezo_layers),expmat(dy_tmp,nbr_piezo_layers));
    data = correct_line_shift(data,mean(data,3));
    for rnd=1:nbr_piezo_layers
        act_map{rnd}=calc_act_map(data(:,:,1:nbr_piezo_layers:end));
        template{rnd}=mean(data(:,:,1:nbr_piezo_layers:end),3);
    end
else
    act_map={};
    template={};
    for rnd=1:length(dx)
        data{rnd}=shift_data(data{rnd},dx{rnd},dy{rnd});
        data{rnd}=correct_line_shift(data{rnd},mean(data{rnd},3));
        act_map{rnd}=calc_act_map(data{rnd});
        template{rnd}=mean(data{rnd},3);
    end
end

% write adata file
if nbr_piezo_layers<2
    ROIs=struct;
    bv=struct;
    np=struct;
    save_adata(adata_dir,ROIs,bv,np,template,dx,dy,ExpInfo.aux_files,ExpInfo.fnames,nbr_frames,ExpInfo.mouse_id,ExpInfo.userID,act_map);
else
    ROIs=cell(1,nbr_piezo_layers);
    bv=cell(1,nbr_piezo_layers);
    np=cell(1,nbr_piezo_layers);
    for wnd=1:nbr_piezo_layers
        ROIs{wnd}=struct;
        bv{wnd}=struct;
        np{wnd}=struct;
    end
    save_adata(adata_dir,ROIs,bv,np,template,dx,dy,ExpInfo.aux_files,ExpInfo.fnames,nbr_frames,ExpInfo.mouse_id,ExpInfo.userID,act_map);
end

disp('----Done registering Exp!----');
%EOF